package com.test;

import com.entity.TestStudent;
import org.apache.ibatis.annotations.Select;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Project: tobe-better-coder
 * Package: com.test
 *
 * @author : Mr.xu
 * @date : 2021/10/2 21:03
 */
interface UserMapper {
    @Select("select * from student")
    public void selectAll();

    public void selectOne();
}

public class Application {
    static int flag = 0;

    public static void main(String[] args) {
        UserMapper userMapper = (UserMapper) Proxy.newProxyInstance(Application.class.getClassLoader(), new Class<?>[]{UserMapper.class}, new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                flag++;
                System.out.println(method.getName());
                System.out.println("JDK动态代理");
                System.out.println("flag = " + flag);
                Select select = method.getAnnotation(Select.class);
                System.out.println(Arrays.toString(select.value()).replace("[", "").replace("]", ""));
                //我这里是直接把sql写在了注解上，mybatis是和方法绑定，调用的时候通过动态代理找到方法名，参数，再找到对应的sql，再把参数塞进去，最终执行sql封装结果
                String sql = Arrays.toString(select.value()).replace("[", "").replace("]", "");
                JDBCImplement(sql);
                return null;
            }
        });
        //动态代理，每次调用方法的时候，都会走一遍代理
//        System.out.println(userMapper.selectAll());;
//        userMapper.selectOne();
    }

    public static void JDBCImplement(String sql) {
        String url = "jdbc:mysql://localhost:3306/com.test";
        String username = "root";
        String password = "xuxin";
        List<TestStudent> list = new ArrayList<>();
        //新的mysql驱动，不需要自己手动加载：com.mysql.cj.jdbc.Driver
//        try {
//            Class.forName("com.mysql.jdbc.Driver");
//        }catch (Exception e){
//            e.printStackTrace();
//        }
        Connection con = null;
        Statement statement = null;
        ResultSet set = null;
        try {
            con = DriverManager.getConnection(url, username, password);
            statement = con.createStatement();
            set = statement.executeQuery(sql);
            while (set.next()) {

                TestStudent student = new TestStudent();
                student.setId(set.getString(1));
                student.setName(set.getString(2));
                student.setSex(set.getString(3));
                student.setAge(set.getInt(4));
                student.setBanji(set.getString(5));
                list.add(student);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (con != null) {
                try {
                    con.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (statement != null) {
                    try {
                        statement.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                if (set != null) {
                    try {
                        set.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        System.out.println(list.toString());
    }
}